package com.jhs.kjs.handler;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.jhs.kjs.dto.PaingDTO;

public class ReportExcelViewHandler extends AbstractExcelView {
	@Override
	protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,HttpServletRequest request, HttpServletResponse response) throws Exception {
		HSSFSheet sheet = createFirstSheet(wb);
		createColumnLabel(wb, sheet);
		
		@SuppressWarnings("unchecked")
		List<PaingDTO> carList = (List<PaingDTO>)model.get("list");
		for(int i=0; i < carList.size(); i++) {
			createPageRow(wb, sheet, carList.get(i), i);
		}
		
		String fileInfo = String.format("attachment; filename=\"" + createFileName() + "\"");
		response.setHeader("Content-Disposition", fileInfo);
	}
	
	private String createFileName() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		return new StringBuilder("Report-").append(dateFormat.format(new Date())).append(".xls").toString();
	}
	private HSSFSheet createFirstSheet(HSSFWorkbook workbook) {
		HSSFSheet sheet = workbook.createSheet();
		workbook.setSheetName(0, "운행기록");
		int col=0;

		sheet.setColumnWidth(col++,2000);	//번호
		sheet.setColumnWidth(col++,3000);	//시동
		sheet.setColumnWidth(col++,6000);	//수신시각
		sheet.setColumnWidth(col++,8000);	//위치
		sheet.setColumnWidth(col++,3000);	//속도
		sheet.setColumnWidth(col++,3000);	//rpm
		sheet.setColumnWidth(col++,4000);	//방향
		sheet.setColumnWidth(col++,3000);	//제동
		sheet.setColumnWidth(col++,3000);	//거리
		sheet.setColumnWidth(col++,3000);	//누적거리
		sheet.setColumnWidth(col++,2000);	//CDMA
		sheet.setColumnWidth(col++,3000);	//GPS
		sheet.setColumnWidth(col++,2500);	//전압
		sheet.setColumnWidth(col++,8000);	// 이벤트메시지
		
		return sheet;
	}
	
	@SuppressWarnings("deprecation")
	private void createColumnLabel(HSSFWorkbook wb, HSSFSheet sheet) {
		HSSFRow firstRow = sheet.createRow(0);
		HSSFCellStyle titleStyle = wb.createCellStyle();
		
		titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);		

		// 타이틀 설정
		String[] titleList = { "번호", "시동", "수신시각", "위치","속도(Km/h)","RPM","방향","제동","거리(Km)","누적거리(Km)","CDMA","GPS","전압(V)","이벤트메세지"};
		
		for (int i = 0; i < titleList.length; i++) {
			HSSFCell cell = firstRow.createCell(i);
			cell.setCellValue(new HSSFRichTextString(titleList[i]));
			cell.setCellStyle(titleStyle);
		}
//														
	}

	@SuppressWarnings("deprecation")
	private void createPageRow(HSSFWorkbook wb, HSSFSheet sheet,  PaingDTO  carListInfo, int rowNum) {
		int index=0;
		HSSFRow row = sheet.createRow(rowNum +1);
		
		HSSFCell cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getSeq());
		
		cell = row.createCell(index++);
		String startUp = carListInfo.getStartUp();
		if(startUp==null || "".equals(startUp.trim())) startUp = "n";
		cell.setCellValue("y".equals(startUp.toLowerCase())?"On":"Off");
		
		
		HSSFCellStyle dateStyle = wb.createCellStyle();
		dateStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);	
		dateStyle.setDataFormat(wb.createDataFormat().getFormat("yyyy-MM-dd a hh:mm:ss"));
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd a hh:mm:ss");
		
		cell = row.createCell(index++);
		cell.setCellValue(new HSSFRichTextString(dateFormat.format(carListInfo.getDetectTime())));
		cell.setCellStyle(dateStyle);
		
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getAddress());
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getSpeed()==null?"0":carListInfo.getSpeed()+"");
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getRpm()==null?"":carListInfo.getRpm()+"");
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getCardinalStr());
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getBreaked()==null?"":carListInfo.getBreaked()+"");
		
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getDayDistance()==null?"0":carListInfo.getDayDistance()/10+"");
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getTotalDistance()==null?"0":carListInfo.getTotalDistance()/10+"");
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getCDMAState());
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getGPSState());
		
		cell = row.createCell(index++);
		cell.setCellValue(carListInfo.getVoltage()==null?"0":carListInfo.getVoltage().floatValue()/10F+"");
		
		cell = row.createCell(index++);
		cell.setCellValue("-");
	}
}
